package pikes.peak;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Block;
import pikes.html.xhtml.form.Form;
import pikes.uri.URI;

public class FormPanel implements BlockComponent {

	private RequestProperty<URI> uri = new SelfURI();

	private List<Selector<BlockComponent>> selectors = new LinkedList<Selector<BlockComponent>>();

	public Block createHtml(Map model, RequestContext requestContext) throws Exception {
		Form form = new Form(uri.getValue(model, requestContext), Form.Method.POST);

		List<BlockComponent> components = new LinkedList<BlockComponent>();

		for (Selector<BlockComponent> selector : selectors) {
			selector.select(components, model, requestContext);
		}

		for (BlockComponent component : components) {
			form.add(component.createHtml(model, requestContext));
		}

		return form;
	}

	public void add(BlockComponent component) {
		if (component != null) {
			selectors.add(new Selector<BlockComponent>(component));
		}
	}

	public void add(BlockComponent component, VisibilityCondition visibilityCondition) {
		if (component != null) {
			selectors.add(new Selector<BlockComponent>(visibilityCondition, component));
		}

	}

}
